﻿using Notepad.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Notepad.Helper
{
    public class XmlHelper
    {
        public void CreateXml(string fileName)
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "gb2312", null);
            doc.AppendChild(dec);
            XmlElement nodes = doc.CreateElement("Nodes");
            XmlElement colorNode = doc.CreateElement("BackColor");
            colorNode.InnerText = "#FF4A9595";
            nodes.AppendChild(colorNode);
            XmlElement contentNode = doc.CreateElement("Content");
            contentNode.InnerText = "";
            nodes.AppendChild(contentNode);
            XmlElement posNode = doc.CreateElement("Position");
            XmlElement posNodeX = doc.CreateElement("PosX");
            posNodeX.InnerText = "10";
            posNode.AppendChild(posNodeX);
            XmlElement posNodeY = doc.CreateElement("PosY");
            posNodeY.InnerText = "10";
            posNode.AppendChild(posNodeY);
            nodes.AppendChild(posNode);

            doc.AppendChild(nodes);
            doc.Save(System.IO.Path.Combine(CommonHelper.SavePath, fileName + ".xml"));
        }

        /// <summary>
        /// 获取文件内容
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public Nodes GetXmlContent(string path)
        {
            Nodes node = new Nodes();
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            node.BackColor = GetSingleNode(doc, "BackColor").InnerText;
            node.Content = GetSingleNode(doc, "Content").InnerText;
            var pos = GetSingleNode(doc, "Position");

            node.PosX = double.Parse(pos.SelectSingleNode("PosX").InnerText);
            node.PosY = double.Parse(pos.SelectSingleNode("PosY").InnerText);
            return node;
        }

        private XmlNode GetSingleNode(XmlDocument doc, string node)
        {
            var baseNode = doc.SelectSingleNode("Nodes");
            return baseNode.SelectSingleNode(node);
        }

        public void SetNodeValue(string fileName, string node, string value)
        {
            var path = System.IO.Path.Combine(CommonHelper.SavePath, fileName + ".xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            switch(node)
            {
                case "BackColor":
                    GetSingleNode(doc, "BackColor").InnerText = value;
                    break;
                case "Content":
                    GetSingleNode(doc, "Content").InnerText = value;
                    break;
                case "Position":
                    var xva = value.Split(',');
                    var pos = GetSingleNode(doc, "Position");
                    pos.SelectSingleNode("PosX").InnerText = xva[0];
                    pos.SelectSingleNode("PosY").InnerText = xva[1];
                    break;
            }
            doc.Save(path);
        }

    }
}
